// 禁止访问
const urlSearchParams = new URLSearchParams(window.location.search);
const id = urlSearchParams.get("column_id");
if (id === null) {
  window.location.href = "./home3.html";
}
// 获取cookie
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";");
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) === " ") {
        c = c.substring(1, c.length);
      }
      if (c.indexOf(nameEQ) === 0) {
        return c.substring(nameEQ.length, c.length);
      }
    }
    return null;
  }
  // 搜索
document.getElementsByClassName("input")[0].value = getCookie('input');
document.cookie = "input=" + ""

// 渲染左标题
const names = urlSearchParams.get("name");
const leftTop = document.getElementsByClassName("leftTop")[0];
leftTop.innerHTML = names;

// 渲染右侧列表
let page = 1;
function List(e) {
  page = e.target.textContent;
  navLi();
}

function navLi() {
let Search = document.getElementsByClassName("input")[0].value;
const span = document.querySelectorAll(".page");
console.log(span);

// 遍历并删除每个元素
span.forEach((pages) => {
  pages.parentNode.removeChild(pages);
});

  const formTdDivs = document.querySelectorAll("div.moreLi");
  // 遍历并删除每个元素
  formTdDivs.forEach((formTdDiv) => {
    formTdDiv.parentNode.removeChild(formTdDiv);
  });
  let data;
  if (Search === "") {
    data = {
      page: page,
      size: 8,
      cms_columnId: id,
    };
  } else {
    data = {
      Search: Search,
      page: page,
      size: 8,
      cms_columnId: id,
    };
  }
  axios
    .get("http://127.0.0.1:3000/api/v1/front/contentList", {
      params: data,
    })
    .then(function (res) {
      if (res.status == 200) {
        if (res.data.code == 0) {
          console.log(res);
          for (let i = 0; i < res.data.data.list.length; i++) {            
            
            let div = document.createElement("div");
            div.className = "moreLi";
            div.innerHTML = `<div class="moreLiLeft">
                            <div class="moreLiLeftTop">
                                19
                            </div>
                            <div class="moerLiLeftBottom">
                                2022-08
                            </div>
                        </div>
                        <div class="moreLiRight">
                            <div class="moreLiRightTop">
                                ${res.data.data.list[i].name}
                            </div>
                            <div class="moreLiRightBottom">
                                ${res.data.data.list[i].content}
                            </div>
                        </div>`;
            const rightTd = document.getElementsByClassName("rightTd")[0];
            rightTd.appendChild(div);
            let moreLi = document.getElementsByClassName('moreLi')[i];
            moreLi.addEventListener('click', function() {
                window.location.href = `./acticle.html?column_id=${id}&name=${names}&id=${res.data.data.list[i].id}`
            })
            total = Math.ceil(res.data.data.totalList[0].total / 8);
            document.cookie = "total=" + total;
          }
          pages()
        }
      }
    })
    .catch(function (error) {
      console.log(error);
    });
}
navLi();




// 翻页器
function pages() {

  let spanL = document.createElement("span");
  spanL.className = 'page';
  spanL.innerHTML = `<span onclick="minus()"><</span>`
  const Pagesss = document.getElementsByClassName('rightPage')[0];
  Pagesss.appendChild(spanL)
    for(let i = 0; i < getCookie('total'); i++) {
        let span = document.createElement("span");
        span.className = 'page'
        span.innerHTML = `<span onclick="List(event)">${i+1}</span>`
        const rightPage = document.getElementsByClassName('rightPage')[0];
        rightPage.appendChild(span)
    }
    let spanR = document.createElement("span");
    spanR.className = 'page';
    spanR.innerHTML = `<span onclick="plus()">></span>`
    Pagesss.appendChild(spanR)
}


function minus() {
  if(page > 1) {
    page--
    navLi()
  }
}

function plus() {
  if(page < getCookie('total')){
    page++
    navLi()
  }
}
    